package com.chookss.home.audit.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.home.audit.clockIn.ClockInAskHolidayDetailActivity;
import com.chookss.home.audit.resign.EntryDetailActivity;
import com.chookss.home.audit.resign.ResignDetailActivity;
import com.chookss.home.audit.upgrade.UpgradeDetailActivity;
import com.chookss.home.entity.WorkNewEntity;
import com.chookss.tools.ClearMoreZeroUtil;
import com.chookss.tools.MyEvent;
import com.chookss.tools.Utils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClockInHolidayRecordAdapter extends BaseQuickAdapter<WorkNewEntity, BaseViewHolderEx> {
    private Activity context;

    public ClockInHolidayRecordAdapter(int layoutResId, Activity context, @io.reactivex.annotations.Nullable List<WorkNewEntity> data) {
        super(layoutResId, data);
        this.context = context;
    }

    @Override
    protected void convert(BaseViewHolderEx helper, WorkNewEntity entity) {
        TextView tvTitle = helper.getView(R.id.tvTitle);
        TextView tvTitleType = helper.getView(R.id.tvTitleType);
        TextView tvTime = helper.getView(R.id.tvTime);
        TextView tvLength = helper.getView(R.id.tvLength);
        TextView tvInit = helper.getView(R.id.tvInit);
        LinearLayout llLength = helper.getView(R.id.llLength);
        tvTitle.setText(entity.getProjectName());
        if (entity.getCacheStatus() == 2) {
            if ("14".equals(entity.getWorkFlowType()) || "15".equals(entity.getWorkFlowType())) {
                llLength.setVisibility(View.GONE);
                tvTime.setText("申请时间：" + entity.getErrorTime());
            } else {
                llLength.setVisibility(View.VISIBLE);
                tvLength.setText(entity.getErrorLength());
                tvTime.setText(entity.getErrorTime());
            }
            if (Utils.isNull(entity.getErrorRemark())) {
                tvInit.setText("无");
            } else {
                tvInit.setText(entity.getErrorRemark());
            }
            tvTitleType.setText("上传失败");
            tvTitleType.setTextColor(context.getResources().getColor(R.color.main_color));
            tvTitleType.setBackgroundResource(R.drawable.shape_main_color_4_1f);
        } else {
            if ("14".equals(entity.getWorkFlowType()) || "15".equals(entity.getWorkFlowType())|| "18".equals(entity.getWorkFlowType())) {
                llLength.setVisibility(View.GONE);
                tvTime.setText("申请时间：" + entity.getCreateTime());
            } else {
                llLength.setVisibility(View.VISIBLE);
                tvLength.setText(ClearMoreZeroUtil.subZeroAndDot(entity.getParams().getDays()) + "天");
                tvTime.setText(entity.getParams().getStartTime() + " - " + entity.getParams().getEndTime());
            }
            if (Utils.isNull(entity.getParams().getRemark())) {
                tvInit.setText("无");
            } else {
                tvInit.setText(entity.getParams().getRemark());
            }
            if ("0".equals(entity.getAuditStatus())) {
                tvTitleType.setText("审批中");
                tvTitleType.setTextColor(Color.parseColor("#FCA817"));
                tvTitleType.setBackgroundResource(R.drawable.shape_fca817_4_1f);
            } else if ("1".equals(entity.getAuditStatus())) {
                tvTitleType.setText("已通过");
                tvTitleType.setTextColor(Color.parseColor("#44D3B3"));
                tvTitleType.setBackgroundResource(R.drawable.shape_44d3b3_4_2f);
            } else {
                if ("3".equals(entity.getAuditStatus())) {

                    if ("18".equals(entity.getWorkFlowType())) {
                        tvTitleType.setText("审批不通过");
                    }else{
                        tvTitleType.setText("废弃");
                    }

                } else {
                    tvTitleType.setText("不通过");
                }
                tvTitleType.setTextColor(Color.parseColor("#F2434D"));
                tvTitleType.setBackgroundResource(R.drawable.shape_f2434d_4_2f);
            }
        }
        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Urls.antiShake.check()) {
                    return;
                }
                Intent intent = null;
                if (entity.getCacheStatus() == 2) {
                    //上传失败
                    Map<String, String> map = new HashMap<>();
                    map.put("ClockInAllRecordActivity_error_position", helper.getItemPosition() + "");
                    EventBus.getDefault().post(new MyEvent(map));

                } else {
                    if ("14".equals(entity.getWorkFlowType())) {
                        intent = new Intent(context, ResignDetailActivity.class);
                        intent.putExtra("data", entity);
                        context.startActivity(intent);
                    } else if ("15".equals(entity.getWorkFlowType())) {
                        intent = new Intent(context, UpgradeDetailActivity.class);
                        intent.putExtra("data", entity);
                        context.startActivity(intent);
                    }  else if ("18".equals(entity.getWorkFlowType())) {
                        intent = new Intent(context, EntryDetailActivity.class);
                        intent.putExtra("data", entity);
                        context.startActivity(intent);
                    } else {
                        intent = new Intent(context, ClockInAskHolidayDetailActivity.class);
                        intent.putExtra("data", entity);
                        context.startActivity(intent);
                    }
                }
            }
        });
    }
}
